package com.niiwoo.civet.trade.service.dubbo.invest;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestOrder;
import com.niiwoo.civet.trade.dao.mapper.credit.CreditProjectInvestorMapperExt;
import com.niiwoo.civet.trade.dao.mapper.orgloan.OrgLoanProjectInvestorMapperExt;
import com.niiwoo.civet.trade.dto.common.ProjectInvestOrderDTO;
import com.niiwoo.civet.trade.dto.request.InvestAmountStatisticsRequestDTO;
import com.niiwoo.civet.trade.dto.request.InvestOrderListForActivityRequestDTO;
import com.niiwoo.civet.trade.dto.request.InvestOrderStatusRequestDTO;
import com.niiwoo.civet.trade.dto.request.ProjectBorrowerPlanRequestDTO;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceProtocolloRequestDTO;
import com.niiwoo.civet.trade.dto.response.*;
import com.niiwoo.civet.trade.service.invest.InvestOrderDubboService;
import com.niiwoo.civet.trade.service.local.invest.InvestOrderService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Yijian on 2018/7/5.
 */
@Service(version = "1.0.0")
public class InvestOrderDubboServiceImpl implements InvestOrderDubboService {

    private static final Logger logger = LoggerFactory.getLogger(InvestOrderDubboServiceImpl.class);

    @Autowired
    private InvestOrderService investOrderService;

    @Autowired
    private CreditProjectInvestorMapperExt creditProjectInvestorMapperExt;

    @Autowired
    private OrgLoanProjectInvestorMapperExt orgLoanProjectInvestorMapperExt;

    @Override
    public InvestOrderStatisticsResponseDTO queryInvestOrderStatistcs(String userId) {
        InvestOrderStatisticsResponseDTO responseDTO = investOrderService.queryInvestOrderStatistcs(userId);
        return responseDTO;
    }

    @Override
    public PageResponseDTO<InvestOrderStatusResponseDTO> queryInvestOrderList(InvestOrderStatusRequestDTO investOrderStatusRequestDTO) {
        return investOrderService.queryInvestOrderList(investOrderStatusRequestDTO);
    }

    @Override
    public InvestStatusResponseDTO queryInvestOrderDetail(Long projectInvestOrderId){
        return investOrderService.queryInvestOrderDetail(projectInvestOrderId);
    }

    @Override
    public InvestOrderMaxandMinDateResponseDTO getMyInvestOrderMaxandMinDate(String userId,Byte type){
        return investOrderService.getMyInvestOrderMaxandMinDate(userId,type);
    }

    @Override
    public InvestOrderMaxandMinDateResponseDTO getMyInvestOrderMaxandMinDate(String userId, Byte type, Byte queryType) {
        return investOrderService.getMyInvestOrderMaxandMinDate(userId, type, queryType);
    }

    @Override
    public BigDecimal getMaxRate(){
        return investOrderService.getMaxRate();
    }

    @Override
    public UserHasInvestResponseDTO checkUserHasInvest(String userId){
        UserHasInvestResponseDTO responseDTO = new UserHasInvestResponseDTO();
        responseDTO.setHasCreditLoanInvest(creditProjectInvestorMapperExt.selectMyInvestCount(userId) > 0);
        responseDTO.setHasOrgInvest(orgLoanProjectInvestorMapperExt.selectMyInvestCount(userId) > 0);
        return responseDTO;
    }

	@Override
	public InvestProtocolloResponseDTO queryInvestProtocollo(Long orderId, String userId) {
		Assert.notNull(orderId, "合同列表,订单ID不可空");
		Assert.notNull(userId, "合同列表，用户ID不可空");
		return investOrderService.queryInvestProtocollo(orderId, userId);
	}

    @Override
    public PageResponseDTO<ProjectBorrowerPlanResponseDTO> getPlanByProjectId(ProjectBorrowerPlanRequestDTO requestDTO) {
        return investOrderService.getPlanByProjectId(requestDTO);
    }

	@Override
	public InvestOrderListForActivityResponseDTO getInvestOrderListForActivity(InvestOrderListForActivityRequestDTO requestDTO) {
		InvestOrderListForActivityResponseDTO responseDTO = new InvestOrderListForActivityResponseDTO();
		Assert.notNull(requestDTO, "请求参数不能为空");
		Assert.hasText(requestDTO.getUserId(), "用户ID不能为空");
		List<ProjectInvestOrderDTO> projectInvestOrderDTOList = investOrderService.getInvestOrderListForActivity(requestDTO);
		responseDTO.setProjectInvestOrderDTOList(projectInvestOrderDTOList);
		return responseDTO;
	}

    @Override
    public InvestAmountStatisticsResponseDTO myInvestAmountStatistics(InvestAmountStatisticsRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        Assert.hasText(requestDTO.getUserId(), "用户ID不能为空");
        Assert.notNull(requestDTO.getQueryType(), "查询产品类型不能为空");
        return investOrderService.myInvestAmountStatistics(requestDTO);
    }

    @Override
    public BigDecimal getTodayReturnMoney(InvestAmountStatisticsRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        Assert.hasText(requestDTO.getUserId(), "用户ID不能为空");
        return investOrderService.getTodayReturnMoney(requestDTO);
    }

    @Override
    public InvestProtocolloResponseDTO queryIntelligenceInvestProtocollo(IntelligenceProtocolloRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        Assert.hasText(requestDTO.getUserId(), "用户ID不能为空");
        Assert.notNull(requestDTO.getPackageInvestOrderId(), "包订单ID不能为空");
        return investOrderService.queryIntelligenceInvestProtocollo(requestDTO);
    }

    @Override
    public Long getProjectIdByInvestOrderId(Long investOrderId) {
        ProjectInvestOrder investOrder = investOrderService.getInvestByInvestOrderId(investOrderId);
        return investOrder.getProjectId();
    }
}
